<?php
//
//-------------------------------------------------
// | CreateTime: 2020/8/25 14:28
//+------------------------------------------------
// | Author: chenxueyuan
//+------------------------------------------------
// | Describe: ...
//+------------------------------------------------

namespace app\admin\controller;

class Article extends Common
{

    public function add(){
        return view();
    }
    public function addhanddle(){
       if($this->request->ispost()){
           $post=$this->request->post();
           $file = request()->file('img');
           if(empty($file)){
               $this->error('请选择文章图片','admin/article/add');
           }else{

               $info=$file->move('upload');
               if($info){
                   $data=$post;
                   dump($info);die;
                   $data['img']=str_replace("\\","/",$info->getSaveName());
                   $data['create_time']=time();
                   $data['update_time']=time();
                   $article_model= model('Article');
                   $article_model->save($data);
                   $this->redirect('admin/article/add');
               }
           }
           // 移动到框架应用根目录/uploads/ 目录下

       }else{
           $this->redirect('admin/article/lst');
       }
    }

    public function lst(){
        $article_model= model('article');
        $article_select=$article_model::paginate(4)->each(function($item, $key){
           $item->tags;
        });
        $this->assign('article_select',$article_select);
        return view();
    }
    public function upd($id=''){
        $article_model=model('article');
        $article_get=$article_model->get($id);
        if(empty($article_get)){
            $this->redirect('admin/article/lst');
        }else{
            $this->assign('article_get',$article_get);
            return view();}
    }

    public function updhanddle(){
        if($this->request->isPost()){
            $post=$this->request->post();
            $file=$this->request->file('img');
            if($file){
                $info=$file->move('upload');
                if($info){
                    $data=$post;
                    $article_model= model('Article');
                    $article_old=$article_model->get($post['id']);
                    $img_old=$article_old['img'];
                    $data['img']=str_replace("\\","/",$info->getSaveName());
                    $data['update_time']=time();
                    $article_model->save($data,$post['id']);
                    if(file_exists('upload/'.$img_old)){//图片是否存在
                        unlink('upload/'.$img_old);
                    }
                    $this->success('更新成功','admin/article/add');
                }else{
                    $this->redirect('admin/article/lst');
                }
            }else{
                $data=$post;
                $article_model= model('Article');
                $data['update_time']=time();
                $article_model->save($data,$post['id']);
                $this->success('更新成功','admin/article/add');
            }
        }else{
            $this->redirect('admin/article/lst');
        }

    }
//获取tag
    public function findTagsAjax(){
        if($this->request->isAjax()){
            $post=$this->request->post();
            $tags=$post['tags'];
            $tags_select=db('tags')->where('name','like',$tags.'%')->select();
            return $tags_select;
        }else{
            $this->redirect('admin/article/lst');
        }
    }
    //添加tag
    public function articleTagsAjax(){
        if($this->request->isAjax()){
            $post= $this->request->post();
            $article_id=$post['id'];
            $name=$post['value'];//var_dump($article_id);var_dump($name);die;
            $tags_find = db('tags')->where('name',$name)->find();
            if ($tags_find){
                $tags_find_id=$tags_find['id'];
                $article_tags_find=db('article_tags')->where('article_id',$article_id)->where('tags_id',$tags_find_id)->find();
                if ($article_tags_find){
                    return array('status'=>'-1');
                }else{

                    $article_get=\app\admin\model\Article::get($article_id);

                    $article_tags_result=$article_get->Tags()->save($tags_find_id);
                    if($article_tags_result){
                        return array('status'=>1);
                    }else{
                        return array('status'=>'-2');
                    }
                }
            }else{
                return array('status'=>0);
            }
        }else{
            $this->redirect('admin/article/lst');
        }
    }
    //无刷新删除文章
    public function articleTagsDelAjax(){
        if($this->request->isAjax()) {
            $post = $this->request->post();
            $article_model=model('article');
            $article_get= $article_model->get($post['article_id']);
            $articleTagsResulet=$article_get->Tags()->detach($post['tags_id']);
            if($articleTagsResulet==1){
                return true;
            }else{
                return false;
            }
        }
    }
}